layui.define(['table', 'form','table'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form,table = layui.table;

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/bxscn/device/portable_battery/type/day_cost/item/list.data'
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'leaseDay', title: '租期(天)'}
            ,{field: 'moneyStr', title: '租金(元)'}
            ,{title: '操作', width: 160, align:'center', fixed: 'right',templet:function (record){
                    var html = ' <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update">修改</a>';
                    html = html + ' <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                    return html;
                }}
        ]]
        ,page: false
        ,text: '对不起，加载出现异常！'
    });
  
  //事件
  var active = {
      add: function () {
          layer.open({
              type: 2
              , title: '新增租期阶梯费用'
              , content: layui.setter.webRoot + '/bxscn/device/portable_battery/type/day_cost/item/add.html'
              , maxmin: false
              , area: ['400px', '300px']
              , btn: ['确定', '取消']
              , yes: function (index, layero) {
                  var iframeWindow = window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/portable_battery/type/day_cost/item/save.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  layer.close(index); //关闭弹层
                                  layer.msg('处理成功，点击下方保存后正式生效', {icon: 1});
                                  table.reload('LAY-table');
                              } else {
                                  layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      , update: function (obj) {
          layer.open({
              type: 2,
              title: '修改租期阶梯费用',
              content: layui.setter.webRoot + '/bxscn/device/portable_battery/type/day_cost/item/update.html?leaseDay=' + obj.data.leaseDay,
              maxmin: false,
              area: ['400px', '300px'],
              btn: ['确定', '取消'],
              yes: function (index, layero) {
                  var iframeWindow = window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/portable_battery/type/day_cost/item/save.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  layer.close(index); //关闭弹层
                                  layer.msg('处理成功，点击下方保存后正式生效', {icon: 1});
                                  table.reload('LAY-table');
                              } else {
                                  layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,del: function (obj) {
          layer.confirm('确定要删除该租期阶梯费用吗？', function (index) {
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/bxscn/device/portable_battery/type/day_cost/item/del.do',
                  data: {leaseDay: obj.data.leaseDay},
                  dataType: "json",
                  success: function (res) {
                      if (res.code == 200) {
                          layer.msg('处理成功，点击下方保存后正式生效', {icon: 1});
                          table.reload('LAY-table');
                      } else {
                          layer.msg(res.msg, {icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('bxscn_battery_type_day_cost', {})
});